<?php

namespace App\Filament\Resources\CategoryResource\Pages;

use App\Filament\Resources\CategoryResource;
use App\Models\Category;
use Filament\Actions as FilamentActions;
use Filament\Actions\Action;
use Filament\Resources\Pages\Page;

class TreeList extends Page
{
    protected static string $resource = CategoryResource::class;

    protected static string $view = 'filament.resources.category-resource.pages.tree-list';

    protected static ?string $navigationIcon = 'heroicon-o-document-text';
    protected static ?string $modelLabel = '分类总览';

    protected static ?string $title = '分类总览';

    protected static ?string $navigationGroup = '图书管理';

    public function mount(): void
    {
        static::authorizeResourceAccess();
    }

    public function getViewData(): array
    {
        return [
            'categories' => $this->getCategories()
        ];

    }


    private function getCategories()
    {
        return Category::all()->toTree();
    }
    protected function getHeaderActions(): array
    {
        return [
            //Actions\CreateAction::make(),
        ];
    }
//    public function editAction(): Action
//    {
//        return Action::make('edit')
//            ->label('编辑')
//            ->icon('heroicon-m-pencil-square')
//            ->link()
//            ->url(fn(array $arguments) => static::getResource()::getUrl('edit', ['record' => static::getModel()::find($arguments['data'])]));
//    }

    public function deleteAction(): Action
    {
        return Action::make('delete')
            ->label('删除')
            ->icon('heroicon-m-trash')
            ->requiresConfirmation()
            ->link()
            ->color('danger')
            ->action(fn (array $arguments) => static::getModel()::where('id',$arguments['data'])->delete());
    }
}
